﻿<%@ WebHandler Language="C#" Class="xinxi_yonghu" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_yonghu : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["yongHuType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_yonghu");
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string dengluming = context.Request.Form["dengluming"];
                        string denglumima = context.Request.Form["denglumima"];
                        string yonghuming = context.Request.Form["yonghuxingming"];
                        string yonghujuese = context.Request.Form["yonghujuese"];
                        string yonghuID = context.Request.Form["yongHuID"];
                        if (string.IsNullOrEmpty(yonghuID))
                        {
                            //新增 
                            string createCondition = m_DB.CreateAndCondition("yonghudengluming", DataValueCommonType.字符串, dengluming, "", SearchConditionType.等于);
                            DataTable yongHuDt = m_DB.GetDataTableByCondition("tbl_yonghu", createCondition);
                            if (yongHuDt.Rows.Count > 0)
                            {
                                context.Response.Write("存在同名用户，请检查后重试...");
                                return;
                            }

                            TableRow newRow = new TableRow("tbl_yonghu", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "yonghudengluming";
                            col1.ValueMember = dengluming;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "yonghuxingming";
                            col2.ValueMember = yonghuming;
                            col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "yonghumima";
                            col3.ValueMember = KeaoUtils.Encrypt(denglumima);
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "yonghujuese";
                            col4.ValueMember = yonghujuese;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "shangchuanzhuangtai";
                            col5.ValueMember = "1";
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "zhandian";
                            col6.ValueMember = "云端";
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "kehubianhao";
                            col7.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }

                        }
                        else
                        {
                            //修改 
                            string createCondition = m_DB.CreateAndCondition("yonghudengluming", DataValueCommonType.字符串, dengluming, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, yonghuID, "", SearchConditionType.不等于);
                            DataTable yongHuDt = m_DB.GetDataTableByCondition("tbl_yonghu", createCondition);
                            if (yongHuDt.Rows.Count > 0)
                            {
                                context.Response.Write("存在同名用户，请检查后重试...");
                                return;
                            }

                            TableRow newRow = new TableRow("tbl_yonghu", yonghuID, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "yonghudengluming";
                            col1.ValueMember = dengluming;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "yonghuxingming";
                            col2.ValueMember = yonghuming;
                            col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col4 = new TableCol();
                            col4.ColName = "yonghujuese";
                            col4.ValueMember = yonghujuese;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "shangchuanzhuangtai";
                            col5.ValueMember = "1";
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "zhandian";
                            col6.ValueMember = "云端";
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "kehubianhao";
                            col7.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                    case "3":
                        string delYongHuID = context.Request.Form["yongHuID"];
                        if (string.IsNullOrEmpty(delYongHuID))
                        {
                            context.Response.Write("没有找到用户信息，请检查重试...");
                            return;
                        }
                        else
                        {
                            TableRow delRow = new TableRow();
                            delRow.TableName = "tbl_yonghu";
                            delRow.ID = delYongHuID;
                            if (m_DB.DeleteRowByID(delRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("用户信息异常。{0}", err.Message));
                  context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}